unit U_MesaiMolaGirisi;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FormClassEkran, ImgList, XPMan, StdActns, ActnList, fcStatusBar,
  StdCtrls, Buttons, ExtCtrls, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, fcTreeView, Mask, DBCtrls,
  dxmdaset, ADODB, Grids, Wwdbigrd, Wwdbgrid, DBGrids, cxDBLookupComboBox,
  cxContainer, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit,
  cxDBLookupEdit, Menus;

type
  TfmMesaiMolaGirisi = class(TfmFormClassEkran)
    Panel10: TPanel;
    GroupBox12: TGroupBox;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label6: TLabel;
    DBEdit16: TDBEdit;
    DBEdit17: TDBEdit;
    DBEdit18: TDBEdit;
    DBEdit19: TDBEdit;
    DBEdit20: TDBEdit;
    DBEdit1: TDBEdit;
    Panel6: TPanel;
    GroupBox5: TGroupBox;
    Tree: TfcTreeView;
    Panel1: TPanel;
    grdListe: TcxGrid;
    grdListeDBTableView1: TcxGridDBTableView;
    grdListeLevel1: TcxGridLevel;
    MemTable1: TdxMemData;
    aqEntry: TADOQuery;
    dsEntry: TDataSource;
    dsMemTable1: TDataSource;
    aqPersonel: TADOQuery;
    aqMenu: TADOQuery;
    dsMenu: TDataSource;
    dsPersonel: TDataSource;
    ImageList1: TImageList;
    aqMesaiMola: TADOQuery;
    dsMesaiMola: TDataSource;
    aqPersonelList: TADOQuery;
    dsPersonelList: TDataSource;
    PopupMenu1: TPopupMenu;
    Cikar1: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure aqEntryAfterOpen(DataSet: TDataSet);
    procedure FormCreate(Sender: TObject);
    procedure grdListeDBTableView1DragOver(Sender, Source: TObject; X,
      Y: Integer; State: TDragState; var Accept: Boolean);
    procedure grdListeDBTableView1DragDrop(Sender, Source: TObject; X,
      Y: Integer);
    procedure aqMenuAfterOpen(DataSet: TDataSet);
    procedure grdListeDBTableView1CustomDrawCell(
      Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure aqMesaiMolaAfterOpen(DataSet: TDataSet);
    procedure Cikar1Click(Sender: TObject);
    procedure TreeClick(Sender: TObject);
    procedure grdListeDBTableView1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure acKaydetYeniExecute(Sender: TObject);
    procedure grdListeDBTableView1ColumnHeaderClick(
      Sender: TcxGridTableView; AColumn: TcxGridColumn);
  private
    { Private declarations }
    procedure DataSourceAta(DataSource: TDataSource);
    procedure TrCrtAdd(TrStData, caption: ShortString; imgindex, stdindex:
      integer; stdata, stdata2: ShortString);
    procedure TrCrtChl(TrStData, caption: ShortString; imgindex, stdindex:
      integer; stdata, stdata2: ShortString);
  public
    { Public declarations }
  end;

var
  fmMesaiMolaGirisi: TfmMesaiMolaGirisi;

implementation
uses data, FormClass, FormClassEmptyList, Boxes, UnitOrtak, UnitTeknik, Math,
  U_MolaTanimKart;

{$R *.dfm}


procedure TfmMesaiMolaGirisi.TrCrtAdd(TrStData, caption: ShortString; imgindex, stdindex:
  integer; stdata, stdata2: ShortString);
var
  i: integer;
  Node: Tfctreenode;
begin
  for i := 0 to Tree.Items.Count - 1 do
  begin
    if Tree.Items[i].StringData = stdata then
      Exit;
  end;

  Node := tree.Items.Add(tree.TopItem, caption);
  Node.ImageIndex := imgindex;
  Node.StateIndex := -1;
  Node.SelectedIndex := stdindex;
  Node.StringData := stdata;
  Node.StringData2 := stdata;
end;

procedure TfmMesaiMolaGirisi.TrCrtChl(TrStData, caption: ShortString; imgindex, stdindex:
  integer; stdata, stdata2: ShortString);
var
  i: integer;
  Node, Node2: Tfctreenode;
begin
  for i := 0 to Tree.Items.Count - 1 do
  begin
    if Tree.Items[i].StringData = stdata then
      Exit;
  end;

  for i := 0 to Tree.Items.Count - 1 do
  begin
    if Tree.Items[i].StringData = TrStData then
    begin
      Node := Tree.Items[i];
      Node2 := Tree.Items.AddChild(Node, caption);
      Node2.ImageIndex := imgindex;
      Node2.StateIndex := -1;
      Node2.SelectedIndex := stdindex;
      Node2.StringData := stdata;
      Node2.StringData2 := stdata2;
      break;
    end;
  end;
end;

procedure TfmMesaiMolaGirisi.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
  fmMesaiMolaGirisi := nil;
end;

procedure TfmMesaiMolaGirisi.FormShow(Sender: TObject);
begin
  inherited;
  OpenQueryOnDataSet(aqEntry.SQL.Text, aqEntry);
  OpenQueryOnDataSet(aqMesaiMola.SQL.Text, aqMesaiMola);
end;

procedure TfmMesaiMolaGirisi.aqEntryAfterOpen(DataSet: TDataSet);
var
  i: Integer;
  field: TField;
begin
  inherited;
  MemTable1.Close;
  MemTable1.Fields.Clear;
  DataSet.First;
  while not DataSet.Eof do
  begin
    field := TStringField.Create(Self);
    field.Name := 'fdMem' + inttostr(DataSet.RecNo - 1);
    field.FieldName := 'Sablon' + DataSet.FieldByName('id').AsString;
    field.DisplayLabel := DataSet.FieldByName('SablonAdi').AsString;
    field.Tag := DataSet.FieldByName('id').AsInteger;
    field.DefaultExpression := '';
    field.Size := 30;
    field.DataSet := MemTable1;
    DataSet.Next;
  end;
  DataSet.First;
  MemTable1.Open;

  if MemTable1.FieldCount <> 0 then
  begin
    MemTable1.Append;
    for i := 0 to MemTable1.FieldCount - 1 do
      MemTable1.Fields[i].Value := '-1';
    MemTable1.Post;
  end;

  grdListeDBTableView1.ClearItems;
  grdListeDBTableView1.DataController.DataSource := dsMemTable1;
  grdListeDBTableView1.DataController.CreateAllItems;

  if grdListeDBTableView1.ItemCount > 0 then
  begin
    for i := 0 to grdListeDBTableView1.ItemCount - 1 do
    begin
      grdListeDBTableView1.Items[i].PropertiesClass := TcxLookupComboBoxProperties;
      with TcxLookupComboBoxProperties(grdListeDBTableView1.Items[i].Properties) do
      begin
        ListSource := dsPersonelList;
        KeyFieldNames := 'Perkod';
        ListFieldNames := 'AdSoyad';
        ListFieldIndex := 1;
      end;
    end;
  end;
end;

procedure TfmMesaiMolaGirisi.FormCreate(Sender: TObject);
begin
  inherited;
  aqPersonel.Close;
  aqPersonel.Parameters.ParamByName('Direktorluk').Value := dm.sifrekontrol.FieldByName('Direktorluk').Value;
  aqPersonel.Parameters.ParamByName('Departman').Value := dm.sifrekontrol.FieldByName('Departman').Value;
  aqPersonel.Parameters.ParamByName('Bolum').Value := dm.sifrekontrol.FieldByName('Bolum').Value;
  aqPersonel.Parameters.ParamByName('Kisim').Value := dm.sifrekontrol.FieldByName('Kisim').Value;
  aqPersonel.Parameters.ParamByName('Seviye').Value := dm.sifrekontrol.FieldByName('Seviye').Value;
  aqPersonel.Parameters.ParamByName('Perkod').Value := null;
  aqPersonel.Open;

  aqMenu.Close;
  aqMenu.Parameters.ParamByName('Direktorluk').Value := dm.sifrekontrol.FieldByName('Direktorluk').Value;
  aqMenu.Parameters.ParamByName('Departman').Value := dm.sifrekontrol.FieldByName('Departman').Value;
  aqMenu.Parameters.ParamByName('Bolum').Value := dm.sifrekontrol.FieldByName('Bolum').Value;
  aqMenu.Parameters.ParamByName('Kisim').Value := dm.sifrekontrol.FieldByName('Kisim').Value;
  aqMenu.Open;

  OpenQueryOnDataSet(aqPersonelList.SQL.Text, aqPersonelList)
end;

procedure TfmMesaiMolaGirisi.grdListeDBTableView1DragOver(Sender,
  Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  inherited;
  Accept := (Source is TfcTreeView);
end;

procedure TfmMesaiMolaGirisi.grdListeDBTableView1DragDrop(Sender,
  Source: TObject; X, Y: Integer);
begin
  inherited;
  if (Source is TfcTreeView) and (Trim(TcxGridRecordCellHitTest((Sender as TcxGridSite).GridView.ViewInfo.GetHitTest(x, y)).Item.Caption) <> '') then
  begin
    aqPersonel.Close;
    aqPersonel.Parameters.ParamByName('Direktorluk').Value := ifthen(StrToint(Copy(Tree.Selected.StringData2, 1, 10)) = 0, Null, Copy(Tree.Selected.StringData2, 1, 10));
    aqPersonel.Parameters.ParamByName('Departman').Value := ifthen(Copy(Tree.Selected.StringData2, 11, 10) = '', Null, Copy(Tree.Selected.StringData2, 11, 10));
    aqPersonel.Parameters.ParamByName('Bolum').Value := ifthen(Copy(Tree.Selected.StringData2, 21, 10) = '', Null, Copy(Tree.Selected.StringData2, 21, 10));
    aqPersonel.Parameters.ParamByName('Kisim').Value := ifthen(Copy(Tree.Selected.StringData2, 31, 10) = '', Null, Copy(Tree.Selected.StringData2, 31, 10));
    aqPersonel.Parameters.ParamByName('Perkod').Value := ifthen(Copy(Tree.Selected.StringData2, 41, 10) = '', Null, Copy(Tree.Selected.StringData2, 41, 10));
    aqPersonel.Parameters.ParamByName('Seviye').Value := dm.sifrekontrol.FieldByName('Seviye').Value;
    aqPersonel.Open;

    while not aqPersonel.Eof do
    begin
      if not aqMesaiMola.Locate('Perkod', aqPersonel.FieldByName('Perkod').AsInteger, []) then
      begin
        aqMesaiMola.Append;
        aqMesaiMola.FieldByName('Perkod').Value := aqPersonel.FieldByName('Perkod').AsInteger;
        aqMesaiMola.FieldByName('Molaid').Value := MemTable1.Fields[TcxGridRecordCellHitTest((Sender as TcxGridSite).GridView.ViewInfo.GetHitTest(x, y)).Item.Index].Tag;
        aqMesaiMola.Post;
      end;
      aqPersonel.Next;
    end;
    aqEntryAfterOpen(aqEntry);
    aqMesaiMolaAfterOpen(aqMesaiMola);
  end;
end;

procedure TfmMesaiMolaGirisi.aqMenuAfterOpen(DataSet: TDataSet);
begin
  inherited;
  aqMenu.First;
  while not aqMenu.Eof do
  begin
    TrCrtAdd(aqMenu.FieldByName('DirektorlukID').AsString, aqMenu.FieldByName('Direktorluk').AsString, 5, 2, aqMenu.FieldByName('DirektorlukID').AsString, FillLeft(aqMenu.FieldByName('DirektorlukID').AsString, '0', 10));
    TrCrtchl(aqMenu.FieldByName('DirektorlukID').AsString, aqMenu.FieldByName('Departman').AsString, 6, 3, aqMenu.FieldByName('DirektorlukID').AsString + aqMenu.FieldByName('DepartmanID').AsString, FillLeft(aqMenu.FieldByName('DirektorlukID').AsString, '0', 10) + FillLeft(aqMenu.FieldByName('DepartmanID').AsString, '0', 10));
    TrCrtchl(aqMenu.FieldByName('DirektorlukID').AsString + aqMenu.FieldByName('DepartmanID').AsString, aqMenu.FieldByName('Bolum').AsString, 7, 4, aqMenu.FieldByName('DirektorlukID').AsString + aqMenu.FieldByName('DepartmanID').AsString + aqMenu.FieldByName('BolumID').AsString, FillLeft(aqMenu.FieldByName('DirektorlukID').AsString, '0', 10) + FillLeft(aqMenu.FieldByName('DepartmanID').AsString, '0', 10) + FillLeft(aqMenu.FieldByName('BolumID').AsString, '0', 10));
    if aqMenu.FieldByName('KisimID').AsInteger > 0 then
      TrCrtchl(aqMenu.FieldByName('DirektorlukID').AsString + aqMenu.FieldByName('DepartmanID').AsString + aqMenu.FieldByName('BolumID').AsString, aqMenu.FieldByName('Kisim').AsString, 9, 8, aqMenu.FieldByName('DirektorlukID').AsString + aqMenu.FieldByName('DepartmanID').AsString + aqMenu.FieldByName('BolumID').AsString + aqMenu.FieldByName('KisimID').AsString, FillLeft(aqMenu.FieldByName('DirektorlukID').AsString, '0', 10) + FillLeft(aqMenu.FieldByName('DepartmanID').AsString, '0', 10) + FillLeft(aqMenu.FieldByName('BolumID').AsString, '0', 10) + FillLeft(aqMenu.FieldByName('KisimID').AsString, '0', 10));
    aqMenu.Next;
  end;

  aqPersonel.First;
  while not aqPersonel.Eof do
  begin
    if aqPersonel.FieldByName('KisimID').AsInteger > 0 then
      TrCrtchl(aqPersonel.FieldByName('DirektorlukID').AsString + aqPersonel.FieldByName('DepartmanID').AsString + aqPersonel.FieldByName('BolumID').AsString + aqPersonel.FieldByName('KisimID').AsString, aqPersonel.FieldByName('AdSoyad').AsString + ' - ' + aqPersonel.FieldByName('Servis').AsString, ifthen(aqPersonel.FieldByName('VarYok').Value = 0, 1, 0), -1, aqPersonel.FieldByName('DirektorlukID').AsString + aqPersonel.FieldByName('DepartmanID').AsString + aqPersonel.FieldByName('BolumID').AsString + aqPersonel.FieldByName('KisimID').AsString + aqPersonel.FieldByName('Perkod').AsString, FillLeft(aqPersonel.FieldByName('DirektorlukID').AsString, '0', 10) + FillLeft(aqPersonel.FieldByName('DepartmanID').AsString, '0', 10) + FillLeft(aqPersonel.FieldByName('BolumID').AsString, '0', 10) + FillLeft(aqPersonel.FieldByName('KisimID').AsString, '0', 10) + aqPersonel.FieldByName('Perkod').AsString)
    else
      TrCrtchl(aqPersonel.FieldByName('DirektorlukID').AsString + aqPersonel.FieldByName('DepartmanID').AsString + aqPersonel.FieldByName('BolumID').AsString, aqPersonel.FieldByName('AdSoyad').AsString + ' - ' + aqPersonel.FieldByName('Servis').AsString, ifthen(aqPersonel.FieldByName('VarYok').Value = 0, 1, 0), -1, aqPersonel.FieldByName('DirektorlukID').AsString + aqPersonel.FieldByName('DepartmanID').AsString + aqPersonel.FieldByName('BolumID').AsString + aqPersonel.FieldByName('KisimID').AsString + aqPersonel.FieldByName('Perkod').AsString, FillLeft(aqPersonel.FieldByName('DirektorlukID').AsString, '0', 10) + FillLeft(aqPersonel.FieldByName('DepartmanID').AsString, '0', 10) + FillLeft(aqPersonel.FieldByName('BolumID').AsString, '0', 10) + FillLeft(aqPersonel.FieldByName('KisimID').AsString, '0', 10) + aqPersonel.FieldByName('Perkod').AsString);
    aqPersonel.Next;
  end;
end;

procedure TfmMesaiMolaGirisi.grdListeDBTableView1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;
  if AViewInfo.GridRecord.Index = 0 then
    ACanvas.Brush.Color := clLime;
end;

procedure TfmMesaiMolaGirisi.aqMesaiMolaAfterOpen(DataSet: TDataSet);
begin
  inherited;
  MemTable1.DisableControls;
  try
    DataSet.First;
    while not DataSet.Eof do
    begin
      MemTable1.First;
      while not MemTable1.Eof do
      begin
        if (MemTable1.FieldByName('Sablon' + DataSet.FieldByName('Molaid').AsString).AsString = '') then
        begin
          MemTable1.Edit;
          MemTable1.FieldByName('Sablon' + DataSet.FieldByName('Molaid').AsString).Value := DataSet.FieldByName('Perkod').Value;
          MemTable1.Post;
          Break;
        end;
        MemTable1.Next;
      end;
      if MemTable1.FieldByName('Sablon' + DataSet.FieldByName('Molaid').AsString).AsString <> DataSet.FieldByName('Perkod').AsString then
      begin
        MemTable1.Append;
        MemTable1.FieldByName('Sablon' + DataSet.FieldByName('Molaid').AsString).Value := DataSet.FieldByName('Perkod').Value;
        MemTable1.Post;
      end;
      DataSet.Next;
    end;
  finally
    MemTable1.EnableControls;
  end;
end;

procedure TfmMesaiMolaGirisi.Cikar1Click(Sender: TObject);
begin
  inherited;
  if grdListeDBTableView1.Controller.FocusedItem.EditValue > 0 then
  begin
    if aqMesaiMola.Locate('Perkod', grdListeDBTableView1.Controller.FocusedItem.EditValue, []) then
    begin
      aqMesaiMola.Delete;
      aqEntryAfterOpen(aqEntry);
      aqMesaiMolaAfterOpen(aqMesaiMola);
    end;
  end;
end;

procedure TfmMesaiMolaGirisi.TreeClick(Sender: TObject);
begin
  inherited;
  if (Tree.Selected.ImageIndex in [0, 1]) and (aqPersonel.Locate('Perkod', Copy(Tree.Selected.StringData2, 41, 10), [])) then
    DataSourceAta(dsPersonel)
  else
    DataSourceAta(nil);
end;

procedure TfmMesaiMolaGirisi.grdListeDBTableView1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if ACellViewInfo.Value <> ' ' then
    if ACellViewInfo.Value > 0 then
    begin
      aqPersonelList.Locate('Perkod', ACellViewInfo.Value, []);
      DataSourceAta(dsPersonelList)
    end
    else
      DataSourceAta(nil);
end;

procedure TfmMesaiMolaGirisi.DataSourceAta(DataSource: TDataSource);
begin
  DBEdit16.Datasource := DataSource;
  DBEdit17.Datasource := DataSource;
  DBEdit18.Datasource := DataSource;
  DBEdit19.Datasource := DataSource;
  DBEdit20.Datasource := DataSource;
end;


procedure TfmMesaiMolaGirisi.acKaydetYeniExecute(Sender: TObject);
begin
  inherited;
  aqMesaiMola.UpdateBatch;
  Close;
end;

procedure TfmMesaiMolaGirisi.grdListeDBTableView1ColumnHeaderClick(
  Sender: TcxGridTableView; AColumn: TcxGridColumn);
begin
  inherited;
  if MemTable1.Active then
  begin
    if not Assigned(fmMolaTanimKart) then
      fmMolaTanimKart := TfmMolaTanimKart.Create(Owner, Self, Self.FormStyle, futInspection, IntToStr(MemTable1.Fields[AColumn.Index].Tag));
    fmMolaTanimKart.Show;
  end;
end;

end.

